package com.fjb.config.listener;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description:监听Session连接状态
 * @author hemiao
 * @time:2020年3月7日 下午4:30:18
 */
public class SessionConnectionStateListener implements ConnectionStateListener{

	private static final Logger log = LoggerFactory.getLogger(SessionConnectionStateListener.class);
	 
    public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState){
        log.info("-----> com.fjb.config.listener.SessionConnectionStateListener");
    	if (connectionState == ConnectionState.LOST) {
    		log.info("ConnectionState: lost");
            try {
                for (;;) {
                    if (curatorFramework.getZookeeperClient().blockUntilConnectedOrTimedOut())
                    {
                    	log.info("reconnection: success");
                        break;
                    }
                }
                return;
            }
            catch (InterruptedException e) {
            	log.info("reconnection: failure");
            }
            catch (Exception e) {
            	log.info("reconnection: failure");
            }
        }
    }
}
